home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ggraph / ggraph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-28  |  5.0 KB  |  188 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <strings.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6. #include "ggraph.h"
  7. #include "ggraphdefs.h"
  8.  
  9. int interact;            /* switch for interactive mode */
  10. main (argc, argv)
  11. char   *argv[];
  12. int     argc;
  13. {
  14. FILE *infile;            /* input file */
  15.  
  16.     if(argc == 3) {        /* scan arguments */
  17.       if(!strcmp(argv[1], "-d")){
  18.     debug++;
  19.     argc--, argv++;
  20.       }
  21.       if(!strcmp(argv[1], "-i")){
  22.     interact++;
  23.     argc--, argv++;
  24.       }
  25.     }
  26.     if (argc == 1) {
  27.     infile = stdin;
  28.     fflush(stdin);
  29.     outfile = stdout;
  30.     }else
  31.     if ((infile = fopen (argv[1], "r")) == NULL) {/* open input file */
  32.     perror ("can't open input file");
  33.     exit (-1);
  34.     }
  35.     if ((outfile = fopen (argv[2], "w")) != NULL)/* open output file */
  36.     setbuf (outfile, NULL);    /* no buffering */
  37.     if(debug)fprintf (stderr, "Reading file\n");
  38.     readcmds (infile);        /* read graph commands */
  39.     if(debug)fprintf (stderr, "Graph program done\n");
  40.     exit (0);
  41. }
  42.  
  43. /****************************************************************
  44.  *                                *
  45.  *    gremlinheader - write header for gremlin file        *
  46.  *                                *
  47.  ****************************************************************/
  48. gremlinheader () {
  49.     if(version == SUN_GREMLIN)
  50.       fprintf (outfile, "%s\n%d %4.1f %4.1f\n",
  51.         sfirstline, VERTICAL, cg.xorigin, cg.yorigin);
  52.     else
  53.       fprintf (outfile, "%s\n%d %4.1f %4.1f\n",
  54.         firstline, VERTICAL, cg.xorigin, cg.yorigin);
  55. }
  56.  
  57. /****************************************************************
  58.  *                                *
  59.  *    drawctext - write some text                 *
  60.  *            x, y - point to draw text at        *
  61.  *            font - font to use                *
  62.  *            size - size of text                *
  63.  *            string - text to draw            *
  64.  *            just - justification to use            *
  65.  *                                *
  66.  ****************************************************************/
  67. drawctext (x, y, font, size, string, just)
  68. int     font;
  69. int     size;
  70. float   x,
  71.         y;
  72. char   *string;
  73. int     just;
  74. {
  75.     float pos_x, pos_y;
  76.     int length;
  77.  
  78.     length = strlen(string) * xcharsz[size];
  79.  
  80.     switch (just) {
  81.     case BOTLEFT_TEXT:
  82.         pos_x = x;
  83.         pos_y = y;
  84.     case BOTCENTER_TEXT:
  85.         pos_x = x - (length >> 1);
  86.         pos_y = y;
  87.         break;
  88.     case BOTRIGHT_TEXT:
  89.         pos_x = x - length;
  90.         pos_y = y;
  91.         break;
  92.     case CENTERLEFT_TEXT:
  93.         pos_x = x;
  94.         pos_y = y - (ycharsz[size] >> 1);
  95.         break;
  96.     case CENTERCENTER_TEXT:
  97.         pos_x = x - (length >> 1);
  98.         pos_y = y - (ycharsz[size] >> 1);
  99.         break;
  100.     case CENTERRIGHT_TEXT:
  101.         pos_x = x - length;
  102.         pos_y = y - (ycharsz[size] >> 1);
  103.         break;
  104.     case TOPLEFT_TEXT:
  105.         pos_x = x;
  106.         pos_y = y + descenders[size] - ycharsz[size];
  107.         break;
  108.     case TOPCENTER_TEXT:
  109.         pos_x = x - (length >> 1);
  110.         pos_y = y + descenders[size] - ycharsz[size];
  111.         break;
  112.     case TOPRIGHT_TEXT:
  113.         pos_x = x - length;
  114.         pos_y = y + descenders[size] - ycharsz[size];
  115.         break;
  116.     }
  117.  
  118.     if (version == SUN_GREMLIN)
  119.     fprintf(outfile, "%s\n", justify_names[just]);
  120.     else
  121.     fprintf(outfile, "%d\n", just);
  122.  
  123.     fprintf(outfile, "%3.2f %3.2f\n", x, y);
  124.     fprintf(outfile, "%3.2f %3.2f\n", pos_x, pos_y);
  125.     fprintf(outfile, "%3.2f %3.2f\n", pos_x + (length >> 1), pos_y);
  126.     fprintf(outfile, "%3.2f %3.2f\n", pos_x + length, pos_y);
  127.  
  128.     fprintf(outfile, (version == SUN_GREMLIN) ? "*\n" : "-1.00 -1.00\n");
  129.     fprintf (outfile, "%d %d\n%d %s\n", font, size, strlen (string), string);
  130.     return(GR_OK);
  131. }
  132.  
  133. /****************************************************************
  134.  *                                *
  135.  *    drawvtext - write some vertical text            *
  136.  *            x, y - point to draw text at        *
  137.  *            font - font to use                *
  138.  *            size - size of text                *
  139.  *            string - text to draw            *
  140.  *            charh - height per character to use        *
  141.  *                                *
  142.  ****************************************************************/
  143. drawvtext (x, y, font, size, string, charh)
  144. int     font;
  145. int     size;
  146. float   x, y;
  147. char   *string;
  148. int   charh;
  149. {
  150.     char *cp;            /* pointer into string */
  151.  
  152.     cp = string;
  153.     while(*cp){
  154.     if (version == SUN_GREMLIN)
  155.     fprintf(outfile, "%s\n", justify_names[TOPCENTER_TEXT]);
  156.     else
  157.     fprintf(outfile, "%d\n",  TOPCENTER_TEXT);
  158.     fprintf (outfile, "%4.1f %4.1f\n", x, y);
  159.     fprintf (outfile, "%4.1f %4.1f\n", x, y);
  160.     fprintf (outfile, "%4.1f %4.1f\n", x + 5.0, y);
  161.     fprintf (outfile, "%4.1f %4.1f\n", x + 10.0, y);
  162.     fprintf(outfile, (version == SUN_GREMLIN) ? "*\n" : "-1.00 -1.00\n");
  163.     fprintf (outfile, "%d %d\n%d %c\n", font, size, 1, *cp);
  164.     ++cp;
  165.     y = y - charh;
  166.     }
  167. }
  168.  
  169. /****************************************************************
  170.  *                                *
  171.  *    drawline - write a line                 *
  172.  *            x1, y1 - starting point            *
  173.  *            x1, y1 - ending point            *
  174.  *                                *
  175.  ****************************************************************/
  176. drawline (brush, x1, y1, x2, y2)
  177. int     brush;
  178. float   x1, x2, y1, y2;
  179. {
  180.     if(version == SUN_GREMLIN)
  181.       fprintf (outfile, "VECTOR\n%4.1f %4.1f\n%4.1f %4.1f\n", x1, y1, x2, y2);
  182.     else
  183.       fprintf (outfile, "%d\n%4.1f %4.1f\n%4.1f %4.1f\n", LINE, x1, y1, x2, y2);
  184.     fprintf(outfile, (version == SUN_GREMLIN) ? "*\n" : "-1.00 -1.00\n");
  185.     fprintf (outfile, "%d %d\n%d\n", brush, 0, 0);
  186. }
  187.  
  188.